package com.fs.sale.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.pojo.Product;
import com.fs.sale.mapper.ProductMapper;
import com.fs.sale.service.ProductService;
import com.fs.sale.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<ProductVo> selectAll() {
        return productMapper.queryAll();
    }

    @Override
    public Page<ProductVo> select(PageDomain pageDomain,ProductVo productVo) {
        //把PageDomain 转换为 Page对象
        Page<ProductVo> page = new Page<>(pageDomain.getPageNum(),pageDomain.getPageSize());
        if(pageDomain.getIsAsc().equalsIgnoreCase("asc")){ //升序
            page.addOrder(OrderItem.asc(pageDomain.getOrderByColumn()));
        }else if(pageDomain.getIsAsc().equalsIgnoreCase("desc")){ //降序
            page.addOrder(OrderItem.desc(pageDomain.getOrderByColumn()));
        }

        List<ProductVo> query = productMapper.query(page, productVo);
        page.setRecords(query);
        return page;
    }

    @Override
    public List<Product> selectProduct() {
        return productMapper.selectList(null);
    }

    @Override
    public List<String> queryBrand() {
        return productMapper.queryBrand();
    }

    @Override
    public List<String> queryProductName(String productName) {
        return productMapper.queryProductName(productName);
    }
}
